<template>
  <div class="container">
    <el-card class="subject-card">
      <!-- 关键字搜索框 -->
      <el-form
        label-width="80px"
        :model="formLabelAlign"
        ref="requestParameters"
      >
        <el-form-item label="学科名称" prop="subjectName">
          <el-input
            @keyup.enter="handleFilter"
            v-model="formLabelAlign.subjectName"
            class="subinput"
          ></el-input>
          <el-button size="small" class="clear" @click="subclear"
            >清除</el-button
          >
          <el-button type="primary" size="small" @click="handleFilter"
            >搜索</el-button
          >
          <el-button
            ative="1"
            type="success"
            icon="el-icon-edit"
            size="small"
            class="newDiscipline"
            @click="subDiscipline = true"
            >新增学科</el-button
          >
        </el-form-item>
      </el-form>
      <!-- /关键字搜索框 -->
      <!-- 数据条数提示 -->
      <el-alert
        class="alert"
        :title="alertText"
        type="info"
        show-icon
        :closable="false"
      ></el-alert>
      <!-- /数据条数提示 -->

      <!-- 表格数据渲染 -->
      <el-table
        :data="subjectList"
        style="width:100%"
        class="tableHell"
        v-loading="listLoading"
        element-loading-text="加载中"
      >
        <el-table-column label="序号" width="50px" type=index>
        </el-table-column>
        <el-table-column label="学科名称" width="150px">
          <template slot-scope="scope">
            <span>{{ scope.row.subjectName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建者" width="140px">
          <template slot-scope="scope">
            <span>{{ scope.row.username }}</span>
          </template></el-table-column
        >
        <el-table-column label="创建日期" width="160px">
          <template slot-scope="scope">
            <span>{{
              scope.row.addDate | parseTimeByString('{y}-{m}-{d}')
            }}</span>
          </template></el-table-column
        >
        <el-table-column label="前台是否显示" width="140px">
          <template slot-scope="scope">
            <span v-if="scope.row.isFrontDisplay == 0">否</span>
            <span v-else>是</span>
          </template></el-table-column
        >
        <el-table-column label="二级目录" width="140px">
          <template slot-scope="scope">
            <span>{{ scope.row.twoLevelDirectory }}</span>
          </template></el-table-column
        >
        <el-table-column label="标签" width="140px">
          <template slot-scope="scope">
            <span>{{ scope.row.tags }}</span>
          </template></el-table-column
        >
        <el-table-column label="题目数量" width="140px">
          <template slot-scope="scope">
            <span>{{ scope.row.totals }}</span>
          </template></el-table-column
        >
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <div class="cell">
              <el-button plain @click="subjectClassify(scope.row)">学科分类</el-button>
              <el-button @click="subjectTag(scope.row)">学科标签</el-button>
              <el-button @click="subUpdateAdd(scope.row)">修改</el-button>
              <el-button @click="subremove(scope.row.id)">删除</el-button>
            </div>
          </template></el-table-column
        >
      </el-table>
      <!-- /表格数据渲染 -->

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="Number(requestParameters.page)"
          :total="Number(total)"
          :page-size="Number(requestParameters.pagesize)"
          :page-sizes="[5, 10, 15, 20]"
          layout=" prev, pager,  next,sizes, jumper"
        ></el-pagination>
      </div>
      <!-- /分页 -->
      <!--增加学科弹窗 -->
      <el-dialog
        title="增加学科"
        :visible.sync="subDiscipline"
        width="30%"
        center
        @close="addDialogClosed"
      >
        <div class="subipt">
          <el-form
            :model="subjectAddList"
            ref="addFromRef"
            label-width="100px"
            class="demo-ruleForm"
            :rules="subjectAddListRules"
          >
            <el-form-item label="学科名称" prop="subjectName">
              <el-input
                placeholder="请输入学科名称"
                type="subjectName"
                v-model="subjectAddList.subjectName"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <div class="switchbtn">
              <el-switch
                inactive-text="是否显示"
                v-model="subjectAddList.isFrontDisplay"
                active-color="#13ce66"
                inactive-color="#ff4949"
              >
              </el-switch>
            </div>
            <div class="subbtn">
              <el-button @click="subDiscipline = false">取 消</el-button>
              <el-button type="primary" @click="subjectAdd">确 定</el-button>
            </div>
          </el-form>
        </div>
      </el-dialog>
      <!-- /增加学科弹窗 -->
      <!--修改学科弹窗 -->
      <el-dialog
        title="修改学科"
        :visible.sync="subUpdateamend"
        width="30%"
        center
        @close="addDialogClosed"
      >
        <div class="subipt">
          <el-form
            :model="subjectAddList"
            ref="addFromRef"
            label-width="100px"
            class="demo-ruleForm"
            :rules="subjectAddListRules"
          >
            <el-form-item label="学科名称" prop="subjectName">
              <el-input
                placeholder="请输入学科名称"
                type="subjectName"
                v-model="subjectAddList.subjectName"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <div class="switchbtn">
              <el-switch
                inactive-text="是否显示"
                v-model="subjectAddList.isFrontDisplay"
                active-color="#13ce66"
                inactive-color="#ff4949"
              >
              </el-switch>
            </div>
            <div class="subbtn">
              <el-button @click="subUpdateamend = false">取 消</el-button>
              <el-button type="primary" @click="changeSubject"
                >确 定</el-button
              >
            </div>
          </el-form>
        </div>
      </el-dialog>
      <!-- /弹窗 -->
    </el-card>
  </div>
</template>

<script>
import { list, add, remove, update } from '@/api/hmmm/subjects'

export default {
  name: 'SubjectList',
  data () {
    return {
      ative: '',
      tableKey: 0,
      listLoading: true,
      total: null,
      alertText: '',
      subDiscipline: false,
      subUpdateamend: false,
      subvalue: true,
      subjectList: [],
      value: '',
      label: '',
      isFrontDisplay: 1,
      requestParameters: {
        page: 1,
        pagesize: 10
      },
      formLabelAlign: {
        id: '',
        subjectName: '',
        creator: '',
        addDate: '',
        isFrontDisplay: '',
        twoLevelDirectory: '',
        tags: '',
        totals: ''
      },
      subjectAddList: {
        id: '',
        subjectName: '',
        isFrontDisplay: 1
      },
      subjectAddListRules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    // 初始数据
    this.getList()
  },
  computed: {},
  methods: {
    addDialogClosed () {
      this.subjectAddList.subjectName = ''
      this.subjectAddList.id = ''
      this.subjectAddList.isFrontDisplay = null
    },
    async getList (value, label) {
      this.listLoading = true
      const { data } = await list(this.requestParameters)
      this.subjectList = data.items
      this.total = data.counts
      this.alertText = `共 ${this.total} 条记录`
      this.listLoading = false
      // console.log(data)
    },
    subclear () {
      this.$refs.requestParameters.resetFields()
      // console.log(this)
    },
    // 搜索学科
    async handleFilter () {
      this.listLoading = true
      this.requestParameters.page = 1
      const { data } = await list({
        subjectName: this.formLabelAlign.subjectName
      })
      this.subjectList = data.items
      this.total = data.counts
      this.alertText = `共 ${this.total} 条记录`
      this.listLoading = false
    },
    // 每页显示信息条数
    handleSizeChange (val) {
      this.requestParameters.pagesize = val
      if (this.requestParameters.page === 1) {
        this.getList(this.requestParameters)
      }
    },
    // 进入某一页
    handleCurrentChange (val) {
      this.requestParameters.page = val
      this.getList()
    },
    // 添加学科
    subjectAdd () {
      this.$refs.addFromRef.validate(async valid => {
        if (!valid) return
        // 发起添加请求
        const { data: res } = await add({
          subjectName: this.subjectAddList.subjectName,
          isFrontDisplay: this.subjectAddList.isFrontDisplay ? 1 : 0
        })
        this.subDiscipline = false
        this.getList(res)
      })
    },
    // 修改学科
    subUpdateAdd (row) {
      this.subUpdateamend = true
      console.log(row)
      this.subjectAddList.id = row.id
      this.subjectAddList.isFrontDisplay = !!row.isFrontDisplay
      this.subjectAddList.subjectName = row.subjectName
    },
    // 弹框出发起请求
    changeSubject () {
      this.$refs.addFromRef.validate(async valid => {
        if (!valid) return
        // 发起添加请求
        const { data: res } = await update(
          this.subjectAddList
        )
        this.subUpdateamend = false
        this.getList(res)
      })
    },
    // 删除学科
    async subremove (id) {
      const confirmRestions = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      if (confirmRestions === 'confirm') {
        await remove({ id })
        this.getList()
        this.$message.success('已删除成功')
      }
    },
    // 跳转至学科分类
    subjectClassify (row) {
      this.$router.push({
        path: '/directorys',
        name: 'subjects-directorys',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    },
    // 跳转至学科标签
    subjectTag (row) {
      this.$router.push({
        path: '/tags',
        name: 'subjects-tags',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.subject-card {
  /deep/ .subinput {
    width: 188px;
    height: 32px;
    line-height: 32px;
  }
  .el-input {
    width: 188px;
  }
  .clear {
    margin-left: 10px;
    margin-right: 3px;
  }
  .newDiscipline {
    position: absolute;
    top: 0;
    right: 0;
  }
  .alert {
    margin-bottom: 20px;
  }
  /deep/.cell .el-button {
    margin-right: 3px;
    padding: 0;
    outline: none;
    border-radius: 0;
    background-color: transparent;
    line-height: inherit;
    width: max-content;
    border: none;
    color: #409eff;
    white-space: nowrap;
  }
  .el-table__row {
    margin: 60px 0 !important;
  }
  /deep/.pagination {
    float: right;
    padding: 10px 0;
    margin-top: 10px;
  }
  .demo-ruleForm bottom {
    position: absolute;
    bottom: 5px;
    left: 0;
  }
  .switchbtn {
    margin-left: 30px;
    color: #606266;
  }
  .subbtn {
    margin-top: 60px;
    margin-bottom: -15px;
    margin-left: 100px;
  }
}
</style>
